<?php


namespace App\Helpers;


use App\Models\ActionLog;
use App\Models\Divide\City;
use App\Models\Divide\CitySplitRatio;
use App\Models\Divide\Department;
use App\Models\Divide\DepartmentSplitRatio;
use App\Models\Divide\DepartmentSplitRatioLog;
use App\Models\Divide\PdPdLog;
use App\Models\Divide\PdUserRelationship;
use App\Models\Divide\PersonSplitRatio;
use App\Models\Mch\RegisterCode;
use Carbon\Carbon;

trait CityCommisionTrait
{
    protected function getCity($areacode){
        return CitySplitRatio::query()
            ->select('ratio_id','pid','ratio')
            ->where('area_code',$areacode)->first();
    }
    protected function getCitys($pdOrders,$money){
        $province = $this->getCity($pdOrders->address_code);
        if(!$province){
            info('未获取到城市列表CityCommissionTrait');
            return 0;

        }
        if($province->pid==0){ // 查询到省
            // 获取分配比例
            if($province->ratio>0){ // 表中没有分配比例
                // 查询
                $this->pickCityUser($pdOrders,$province,$money,$province->ratio);
            }
        }else{
            $city = $this->getCity($province->pid);
            if($city->pid==0) {// 查询到市
                if($province->ratio>0){ // 表中没有分配比例
                    // 查询
                    $this->pickCityUser($pdOrders,$province,$money,$province->ratio);
                }
                if($city->ratio>0){ // 表中没有分配比例
                    // 查询
                    $this->pickCityUser($pdOrders,$city,$money,$city->ratio);
                }
            }else{ // 查询到县
                $town = $this->getCity($city->pid);
                if($province->ratio>0){ // 表中没有分配比例
                    // 查询
                    $this->pickCityUser($pdOrders,$province,$money,$province->ratio);
                }
                if($city->ratio>0){ // 表中没有分配比例
                    // 查询
                    $this->pickCityUser($pdOrders,$city,$money,$city->ratio);
                }
                if($town->ratio>0){ // 表中没有分配比例
                    // 查询
                    $this->pickCityUser($pdOrders,$town,$money,$town->ratio);
                }
            }
        }
    }
    protected function pickCityUser($pdOrders,$city_id,$money,$ratios,$type=3){
        if($ratios){
            $provinceUser = PersonSplitRatio::query()
                ->select('user_id')
                ->where('category_id',$pdOrders->account_mode)
                ->where('department_or_city_id',$city_id)
                ->get();
            $num = $provinceUser->count();
            if($num>0){
                $newMoney = $this->divideAmount($money,$num);
                foreach ($provinceUser as $user_id){
                    $user = PdUserRelationship::query()
                        ->where('nid',$user_id->user_id)
                        ->where('role',$type)
                        ->first();
                    // 写入日志
                    $log = [
                        'uid'=>$user->uid,
                        'gid'=>$user->gid,
                        'pid'=>$user->pid,
                        'sid'=>$pdOrders->account_mode,
                        'name'=>$user->name,
                        'money'=>$newMoney,
                        'role'=>$user->role,
                        'order_id'=>$pdOrders->order_id,
                        'order_sid'=>$pdOrders->id,
                        'mid'=>$city_id,
                        'avatar'=>$user->avatar,
                        'level'=>0,
                        'surplusMoney'=>0,
                        'allMoney'=>$user->all_money,
                    ];
                    // 创建日志记录
                    PdPdLog::query()->create($log);

                    if($user){
                        $user->waitting_money = $this->addAmounts($user->waitting_money,$money);
                        $user->save();
                    }
                }
            }

        }
    }
}

